<template>
    <div class="mainContent">
        <ul class="ammeterList">
            <li :class="{'actived': item.id===deviceMsg.id}" v-for="item in deviceList" :key="item.id" @click="changeDevice(item)">{{item.name}}</li>
        </ul>
        <div class="ammeterContent">
            <div class="search">
                <span>日期选择：</span>
                <el-date-picker
                    type="date"
                    v-model="datePicker"
                    :picker-options="options"
                    @change="dateChange"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期">
                </el-date-picker>
                <input type="text" class="hiddenInput" />
            </div>
            <div class="ammeterMsg">
                <div class="code">
                    <div class="value">{{deviceMsg.sn}}</div>
                    <div class="label">设备编号</div>
                </div>
                <div class="code">
                    <div class="value">{{deviceMsg.name}}</div>
                    <div class="label">设备名称</div>
                </div>
                <div class="stateWrap">
                    <div class="value">{{deviceData.signalStrength | isNull}}</div>
                    <div class="label">信号强度</div>
                </div>
                <div class="stateWrap">
                    <div class="value" v-if="deviceData.batteryState===0">正常</div>
                    <div class="value" v-else-if="deviceData.batteryState===1">欠压</div>
                    <div class="value" v-else>--</div>
                    <div class="label">电池状态</div>
                </div>
                <div class="stateWrap">
                    <div class="value">{{deviceData.batteryVol | isNull}}V</div>
                    <div class="label">电池电压</div>
                </div>
                <div class="stateWrap">
                    <div class="value" v-if="deviceData.flowSensorState===0">正常</div>
                    <div class="value" v-else-if="deviceData.flowSensorState===1">故障</div>
                    <div class="value" v-else>--</div>
                    <div class="label">流量传感状态</div>
                </div>
                <div class="turnoverTime">
                    <div class="value">{{deviceData.createTime | isNull}}</div>
                    <div class="label">数据采集时间</div>
                </div>
            </div>
            <div class="ammeterData">
                <div class="dataWrap">
                    <img class="icon" :src="momentFlowIcon" />
                    <div class="label">瞬时流量</div>
                    <div class="num">{{deviceData.transientFlow | isNull}} ㎡/H</div>
                </div>
                <div class="dataWrap">
                    <img class="icon" :src="forwardIcon" />
                    <div class="label">正向流量</div>
                    <div class="num">{{deviceData.forwardFlowValue | isNull}} L</div>
                </div>
                <div class="dataWrap">
                    <img class="icon" :src="runTimeIcon" />
                    <div class="label">运行时间</div>
                    <div class="num">{{deviceData.runtime | isNull}} H</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { format } from "@/sever/utils.js";
import { getEnergyDeviceList, getWaterDetail } from "@/api/energy.js";

export default {
    name: 'ammeterData',
    data() {
        return {
            momentFlowIcon: require("@/assets/images/energy/瞬时流量.png"),
            forwardIcon: require("@/assets/images/energy/正向流量.png"),
            runTimeIcon: require("@/assets/images/energy/运行时间.png"),

            options: {
                disabledDate(date) {
                    return date > Date.now();
                }
            },
            datePicker: format(new Date(), "YYYY-MM-DD"),

            deviceType: 13,
            deptId: "",

            deviceMsg: '',
            deviceList: [],
            deviceData: {},
        };
    },
    computed: {
        porjectObject() {
            return this.$store.state.porjectObject;
        },
    },
    watch: {
        porjectObject(val, oldVal) {
            if (val.deptId != oldVal.deptId) {
                this.deptId = val.deptId;
                this.getDeviceList();
            }
        },
    },
    mounted() {
        this.deptId = this.$store.state.porjectObject.deptId
        this.getDeviceList()
    },
    methods:{
        /* 设备切换 */
        changeDevice(val){
            this.deviceData = {}
            this.deviceMsg = val
            // this.datePicker = format(new Date(), "YYYY-MM-DD")
            this.getDeviceData()
        },

        /* 日期选择 */
        dateChange(val){
            this.deviceData = {}
            this.datePicker = val
            this.getDeviceData()
        },

        /* 设备列表 */
        getDeviceList(){
            let param = {
                deptId: this.deptId,
                type: this.deviceType
            }
            getEnergyDeviceList(param).then(res=>{
                if(res.code == 200){
                    if(res.data.length>0){
                        this.deviceList = res.data
                        this.deviceMsg = res.data[0]
                        this.getDeviceData()
                    }
                }else{
                    this.$Message.warning(res.msg)
                }
            })
        },

        /* 设备详情 */
        getDeviceData(){
            let param = {
                deviceId: this.deviceMsg.id,
                time: this.datePicker
            }
            getWaterDetail(param).then(res=>{
                if(res.code == 200){
                    this.deviceData = res.data ? res.data : {}
                }
            })
        }
    },
    filters:{
        isNull(value){
            if(!value){
                return "--"
            }
            return value
        }
    }
}
</script>
<style scoped lang='less'>

@import "./../../assets/style/themeBase.less";

.mainContent {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    background: #f5f5f5;
    display: flex;
    .ammeterList{
        padding: 10px 20px;
        border-radius: 5px;
        background-color: #fff;
        list-style: none;
        li{
            cursor: pointer;
            padding: 5px;
            width: 200px;
            font-size: 14px;
            color: #606266;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .actived{
            color: var(--themeColor);
        }
    }
    .ammeterContent{
        margin-left: 10px;
        flex: 1;
        .search{
            margin-bottom: 10px;
            padding: 15px 20px;
            border-radius: 4px;
            background-color: #fff;
        }
        .ammeterMsg{
            padding: 15px 0;
            border-radius: 4px;
            background-color: #fff;
            display: flex;
            .code{
                width: 200px;
                border-right: 1px solid #eee;
                box-sizing: border-box;
                text-align: center;
                .label{
                    font-size: 14px;
                    color: #666666;
                    margin-top: 5px;
                }
                .value{
                    font-size: 16px;
                    font-weight: bold;
                    color: #1891ff;
                }
            }
            .stateWrap{
                flex: 1;
                // padding: 0 30px;
                text-align: center;
                .label{
                    font-size: 14px;
                    color: #666666;
                    margin-top: 5px;
                }
                .value{
                    font-size: 16px;
                    color: #1891ff;
                }
            }
            .turnoverTime{
                width: 220px;
                text-align: center;
                .label{
                    font-size: 14px;
                    color: #666666;
                    margin-top: 5px;
                }
                .value{
                    font-size: 16px;
                    color: #1891ff;
                }
            }
        }
        .ammeterData{
            margin-top: 10px;
            display: flex;
            .dataWrap{
                flex: 1;
                margin-right: 10px;
                padding: 40px 20px;
                background-color: #fff;
                border-radius: 4px;
                text-align: center;
                .icon{
                    width: 60px;
                }
                .label{
                    margin-top: 30px;
                    font-size: 18px;
                    color: #333;
                    font-weight: bold;
                }
                .num{
                    margin-top: 30px;
                    font-size: 26px;
                    color: #1891ff;
                    font-weight: bold;
                }
            }
        }
        .emptyBox{
            padding-top: 50px;
        }
    }

    .hiddenInput{
        opacity:0;
        position: absolute;
        top:-9999px;
        width:0;
    }
}
</style>
